define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'commission_log/index' + location.search,
                    add_url: 'commission_log/add',
                    edit_url: 'commission_log/edit',
                    del_url: 'commission_log/del',
                    multi_url: 'commission_log/multi',
                    import_url: 'commission_log/import',
                    table: 'commission_log',
                }
            });

            var table = $("#table");

            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user_id']", form).addClass("selectpage").data("source", "user/user/index").data("primaryKey", "id").data("field", "nickname").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'asc',
                escape: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'type', title: __('Type'), searchList: {"pick":__('Type pick'),"service":__('Type service')}, formatter: Table.api.formatter.status},
                        {field: 'user_nickname', title: __('User'),operate:false},
                        {field: 'user_id', title: __('User_id'),formatter: Table.api.formatter.search,visible:false},
                        {field: 'order_sn', title: __('Order_sn'), operate: 'LIKE'},
                        {field: 'car_number', title: __('Car_number'), operate: 'LIKE'},
                        {field: 'money', title: __('Money'), operate: 'LIKE'},
                        {field: 'servicedtime', title: __('Servicedtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datedate},
                        {field: 'finishtime', title: __('Finishtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datedate},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                    {
                                        name: 'detail',
                                        title: __('详情'),
                                        classname: 'btn btn-xs btn-info btn-dialog',
                                        icon: 'fa fa-eye',
                                        url: 'commission_log/detail',
                                        callback: function (data) {
                                            // console.log('回传结果'+ data)
                                        }
                                    },
                                ],
                            formatter:
                                function (value, row, index) {
                                    var that = $.extend({}, this);
                                    var table = $(that.table).clone(true);
                                    if (row.type == 'pick'){
                                        $(table).data("operate-detail", null);    //移除 详情 按钮
                                    }
                                    that.table = table;
                                    return Table.api.formatter.operate.call(that, value, row, index);
                                }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
